home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWGDIObj.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  13.6 KB  |  487 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWGDIObj.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifdef FW_BUILD_WIN
  13.  
  14. #ifndef FWGDIOBJ_H
  15. #include "FWGDIObj.h"
  16. #endif
  17.  
  18. #ifndef SLGRGLOB_H
  19. #include "SLGrGlob.h"
  20. #endif
  21.  
  22. #ifndef FWODEXCE_H
  23. #includ "FWODExce.h"
  24. #endif
  25.  
  26. // ----- Foundation Includes -----
  27.  
  28. #ifndef FWCOMMON_H
  29. #include <FWCommon.h>
  30. #endif
  31.  
  32. #ifndef FWEXCDEF_H
  33. #include <FWExcDef.h>
  34. #endif
  35.  
  36. #ifndef FWMEMORY_H
  37. #include <FWMemory.h>
  38. #endif
  39.  
  40. //========================================================================================
  41. //    class FW_CPrivWinGDIObject
  42. //========================================================================================
  43.  
  44. FW_DECLARE_AUTO(FW_CPrivWinGDIObject)
  45.  
  46. //----------------------------------------------------------------------------------------
  47. //    FW_CPrivWinGDIObject::FW_CPrivWinGDIObject
  48. //----------------------------------------------------------------------------------------
  49.  
  50. FW_CPrivWinGDIObject::FW_CPrivWinGDIObject() :
  51.     fGDIObject(),
  52.     fStockID(0xFFFF),
  53.     fStockObject(FALSE),
  54.     fDC(NULL),
  55.     fPreviousObject(NULL),
  56.     fChanged(FALSE)
  57. {
  58.     FW_END_CONSTRUCTOR
  59. }
  60.  
  61. //----------------------------------------------------------------------------------------
  62. //    FW_CPrivWinGDIObject::~FW_CPrivWinGDIObject
  63. //----------------------------------------------------------------------------------------
  64.  
  65. FW_CPrivWinGDIObject::~FW_CPrivWinGDIObject()
  66. {
  67.     FW_START_DESTRUCTOR
  68.  
  69.     FW_ASSERT(fDC == NULL);
  70.     FW_ASSERT(fPreviousObject == NULL);
  71.     DeleteGDIObject(fGDIObject);
  72. }
  73.  
  74. //----------------------------------------------------------------------------------------
  75. //    FW_CPrivWinGDIObject::SetStockID
  76. //----------------------------------------------------------------------------------------
  77.  
  78. void FW_CPrivWinGDIObject::SetStockID(int stockID)
  79. {
  80.     if (stockID != fStockID || !fStockObject || fGDIObject == NULL)
  81.     {
  82.         fStockID = stockID;
  83.         fStockObject = TRUE;
  84.         fChanged = TRUE;
  85.     }
  86. }
  87.  
  88. //----------------------------------------------------------------------------------------
  89. //    FW_CPrivWinGDIObject::UnselectObject
  90. //----------------------------------------------------------------------------------------
  91.  
  92. void FW_CPrivWinGDIObject::UnselectObject(HDC hdc)
  93. {
  94.     if (fPreviousObject != NULL)
  95.     {
  96.         FW_ASSERT(fDC == hdc);
  97.         FW_ASSERT(fGDIObject != NULL);    // We can't have fPreviousObject != NULL and fGDIObject == NULL
  98.     
  99.         ::SelectObject(fDC, fPreviousObject);
  100.         fPreviousObject = NULL;
  101.         fDC = NULL;
  102.     }
  103. }
  104.  
  105. //----------------------------------------------------------------------------------------
  106. //    FW_CPrivWinGDIObject::DeleteGDIObject
  107. //----------------------------------------------------------------------------------------
  108.  
  109. void FW_CPrivWinGDIObject::DeleteGDIObject(FW_CPrivWinGDIObjectHandle& GDIObject)
  110. {
  111.     if (GDIObject.fObject != NULL && !GDIObject.fIsStock)
  112.         ::DeleteObject(GDIObject.fObject);
  113.     GDIObject.fObject = NULL;
  114.     GDIObject.fIsStock = FALSE;
  115. }
  116.  
  117. //----------------------------------------------------------------------------------------
  118. //    FW_CPrivWinGDIObject::MakeObject
  119. //----------------------------------------------------------------------------------------
  120.  
  121. void FW_CPrivWinGDIObject::MakeGDIObject(FW_CPrivWinGDIObjectHandle& GDIObject)
  122. {
  123.     GDIObject.fObject = (GDIObject.fIsStock = fStockObject) == TRUE ? ::GetStockObject(fStockID) : CreateObject();
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. //    FW_CPrivWinGDIObject::SelectObject
  128. //----------------------------------------------------------------------------------------
  129. //    Use to select an object into a DC
  130.  
  131. void FW_CPrivWinGDIObject::SelectObject(HDC hdc)
  132. {
  133.     if (fGDIObject == NULL)
  134.     {
  135.         FW_ASSERT(fDC == NULL);
  136.         FW_ASSERT(fPreviousObject == NULL);
  137.         MakeGDIObject(fGDIObject);
  138.         fPreviousObject = ::SelectObject(hdc, fGDIObject);
  139.         FW_ASSERT(fPreviousObject != NULL);
  140.         fDC = hdc;
  141.         fChanged = FALSE;
  142.     }
  143.     else if (fChanged)
  144.     {
  145.         if (fPreviousObject == NULL)
  146.         {
  147.             FW_ASSERT(fDC == NULL);
  148.  
  149.             // ----- We know it is not selected otherwise fPreviousObject would be != NULL -----
  150.             DeleteGDIObject(fGDIObject);
  151.             
  152.             MakeGDIObject(fGDIObject);
  153.             
  154.             fPreviousObject = ::SelectObject(hdc, fGDIObject);
  155.             FW_ASSERT(fPreviousObject != NULL);
  156.             fDC = hdc;
  157.         }
  158.         else
  159.         {
  160.             FW_ASSERT(fDC == hdc);
  161.             
  162.             // ----- We can't delete it before selecting something else -----
  163.             HGDIOBJ hgdiObj = fGDIObject.fObject;
  164.             BOOL bIsStock = fGDIObject.fIsStock;
  165.                         
  166.             MakeGDIObject(fGDIObject);
  167.             fPreviousObject = ::SelectObject(fDC, fGDIObject);
  168.  
  169.             // ----- Now we can delete it -----
  170.             if (!bIsStock)
  171.                 ::DeleteObject(hgdiObj);
  172.         }
  173.         
  174.         fChanged = FALSE;
  175.     }
  176.     else if (fPreviousObject == NULL)    // Never been selected
  177.     {
  178.         FW_ASSERT(fDC == NULL);
  179.         fPreviousObject = ::SelectObject(hdc, fGDIObject);
  180.         FW_ASSERT(fPreviousObject != NULL);
  181.         fDC = hdc;
  182.     }
  183. //    else
  184. //    {
  185. //        FW_ASSERT(FALSE);
  186. //    }
  187. }
  188.  
  189. //----------------------------------------------------------------------------------------
  190. //    FW_CPrivWinGDIObject::GetObject
  191. //----------------------------------------------------------------------------------------
  192. //    Returns an object without selecting it into a DC. Mainly used for brushes
  193.  
  194. HGDIOBJ FW_CPrivWinGDIObject::GetObject(HDC hdc)
  195. {
  196.     FW_ASSERT(hdc != NULL);
  197.     
  198.     if (fGDIObject == NULL)
  199.     {
  200.         FW_ASSERT(fPreviousObject == NULL);
  201.         FW_ASSERT(fDC == NULL);
  202.         MakeGDIObject(fGDIObject);
  203.         fChanged = FALSE;
  204.     }
  205.     else if (fChanged)
  206.     {
  207.         if (fPreviousObject != NULL)
  208.         {
  209.             FW_ASSERT(fDC == hdc);
  210.             FW_ASSERT(fGDIObject != NULL);
  211.             ::SelectObject(fDC, fPreviousObject);
  212.             fPreviousObject = NULL;
  213.             fDC = NULL;
  214.         }
  215.         
  216.         DeleteGDIObject(fGDIObject);
  217.         MakeGDIObject(fGDIObject);
  218.         
  219.         fChanged = FALSE;
  220.     }
  221.     
  222.     return fGDIObject.fObject;
  223. }
  224.  
  225. //========================================================================================
  226. //    class FW_CPrivGDIPen
  227. //========================================================================================
  228.  
  229. //----------------------------------------------------------------------------------------
  230. //    FW_CPrivGDIPen::FW_CPrivGDIPen
  231. //----------------------------------------------------------------------------------------
  232.  
  233. FW_CPrivGDIPen::FW_CPrivGDIPen() :
  234.     FW_CPrivWinGDIObject()
  235. {
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. //    FW_CPrivGDIPen::~FW_CPrivGDIPen
  240. //----------------------------------------------------------------------------------------
  241.  
  242. FW_CPrivGDIPen::~FW_CPrivGDIPen()
  243. {
  244. }
  245.  
  246. //----------------------------------------------------------------------------------------
  247. //    FW_CPrivGDIPen::SetPenSize
  248. //----------------------------------------------------------------------------------------
  249.  
  250. void FW_CPrivGDIPen::SetPenSize(int width)
  251. {
  252.     if (fStockObject || fGDIObject == NULL || width != fWidth)
  253.     {
  254.         fStockObject = FALSE;
  255.         fWidth = width;
  256.         fChanged = TRUE;
  257.     }
  258. }
  259.  
  260. //----------------------------------------------------------------------------------------
  261. //    FW_CPrivGDIPen::SetPenStyle
  262. //----------------------------------------------------------------------------------------
  263.  
  264. void FW_CPrivGDIPen::SetPenStyle(FW_EStyleDash dashStyle)
  265. {
  266.     if (fStockObject || fGDIObject == NULL || dashStyle != fDashStyle)
  267.     {
  268.         fStockObject = FALSE;
  269.         fDashStyle = dashStyle;
  270.         fChanged = TRUE;
  271.     }
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. //    FW_CPrivGDIPen::SetPenColor
  276. //----------------------------------------------------------------------------------------
  277.  
  278. void FW_CPrivGDIPen::SetPenColor(COLORREF color)
  279. {
  280.     if (fStockObject || fGDIObject == NULL || color != fColor)
  281.     {
  282.         fStockObject = FALSE;
  283.         fColor = color;
  284.         fChanged = TRUE;
  285.     }
  286. }
  287.  
  288. //----------------------------------------------------------------------------------------
  289. //    FW_CPrivGDIPen::CreateObject
  290. //----------------------------------------------------------------------------------------
  291.  
  292. HGDIOBJ FW_CPrivGDIPen::CreateObject()
  293. {
  294.     int nStyle = PS_INSIDEFRAME;
  295.     if (fDashStyle != FW_kSolidLine && fWidth <= 1)
  296.     {
  297.         switch (fDashStyle & ~FW_kOpaque)
  298.         {
  299.         default:
  300.             FW_ASSERT(FALSE);        // Invalid dash style
  301.                                     // fall-through
  302.         case FW_kDash:
  303.             nStyle = PS_DASH;
  304.             break;
  305.  
  306.         case FW_kDot:
  307.             nStyle = PS_DOT;
  308.             break;
  309.     
  310.         case FW_kDashDot:
  311.             nStyle = PS_DASHDOT;
  312.             break;
  313.     
  314.         case FW_kDashDotDot:
  315.             nStyle = PS_DASHDOTDOT;
  316.             break;
  317.         }
  318.     }
  319.  
  320.     return ::CreatePen(nStyle, fWidth, fColor);
  321. }
  322.  
  323. //========================================================================================
  324. //    class FW_CPrivGDIBrush
  325. //========================================================================================
  326.  
  327. //----------------------------------------------------------------------------------------
  328. //    FW_CPrivGDIBrush::FW_CPrivGDIBrush
  329. //----------------------------------------------------------------------------------------
  330.  
  331. FW_CPrivGDIBrush::FW_CPrivGDIBrush() :
  332.     FW_CPrivWinGDIObject(),
  333.     fPattern(FW_NEW(FW_CPrivBWPatternRep, (FW_kBlackPat)))
  334. {
  335. }
  336.  
  337. //----------------------------------------------------------------------------------------
  338. //    FW_CPrivGDIBrush::~FW_CPrivGDIBrush
  339. //----------------------------------------------------------------------------------------
  340.  
  341. FW_CPrivGDIBrush::~FW_CPrivGDIBrush()
  342. {
  343. }
  344.  
  345. //----------------------------------------------------------------------------------------
  346. //    FW_CPrivGDIBrush::SetBrushPattern
  347. //----------------------------------------------------------------------------------------
  348.  
  349. void FW_CPrivGDIBrush::SetBrushPattern(const FW_PPrivPattern& pattern)
  350. {
  351.     if (fSolidColorBrush || fStockObject || fGDIObject == NULL || !pattern.IsSameRepAs(fPattern))
  352.     {
  353.         fStockObject = FALSE;
  354.         fSolidColorBrush = FALSE;
  355.         fPattern = pattern;
  356.         fChanged = TRUE;
  357.     }
  358. }
  359.  
  360. //----------------------------------------------------------------------------------------
  361. //    FW_CPrivGDIBrush::SetBrushColor
  362. //----------------------------------------------------------------------------------------
  363.  
  364. void FW_CPrivGDIBrush::SetBrushColor(COLORREF color)
  365. {
  366.     if (color != fColor || fStockObject || fGDIObject == NULL || !fSolidColorBrush)
  367.     {
  368.         fStockObject = FALSE;
  369.         fSolidColorBrush = TRUE;
  370.         fColor = color;
  371.         fChanged = TRUE;
  372.     }
  373. }
  374.  
  375. //----------------------------------------------------------------------------------------
  376. //    FW_CPrivGDIBrush::CreateObject
  377. //----------------------------------------------------------------------------------------
  378.  
  379. HGDIOBJ FW_CPrivGDIBrush::CreateObject()
  380. {
  381.     return fSolidColorBrush ? ::CreateSolidBrush(fColor) : fPattern->WinCreatePatternBrush();
  382. }
  383.  
  384. //----------------------------------------------------------------------------------------
  385. //    FW_CPrivGDIBrush::GetBrush
  386. //----------------------------------------------------------------------------------------
  387. //    Called when framing with a brush
  388.  
  389. HBRUSH FW_CPrivGDIBrush::GetBrush(HDC hdc)
  390. {
  391.     return (HBRUSH) GetObject(hdc);
  392. }
  393.  
  394. //========================================================================================
  395. //    class FW_CPrivGDIFont
  396. //========================================================================================
  397.  
  398. //----------------------------------------------------------------------------------------
  399. //    FW_CPrivGDIFont::FW_CPrivGDIFont
  400. //----------------------------------------------------------------------------------------
  401.  
  402. FW_CPrivGDIFont::FW_CPrivGDIFont() :
  403.     FW_CPrivWinGDIObject()
  404. {
  405. }
  406.  
  407. //----------------------------------------------------------------------------------------
  408. //    FW_CPrivGDIFont::~FW_CPrivGDIFont
  409. //----------------------------------------------------------------------------------------
  410.  
  411. FW_CPrivGDIFont::~FW_CPrivGDIFont()
  412. {
  413. }
  414.  
  415. //----------------------------------------------------------------------------------------
  416. //    FW_CPrivGDIFont::SetFontName
  417. //----------------------------------------------------------------------------------------
  418.  
  419. void FW_CPrivGDIFont::SetFontName(const char* fontName)
  420. {
  421.     if (fStockObject || fGDIObject == NULL || !FW_PrimitiveStringEqual(fFontName, fontName))
  422.     {
  423.         fStockObject = FALSE;
  424.         FW_PrimitiveStringCopy(fontName, fFontName);
  425.         fChanged = TRUE;
  426.     }
  427. }
  428.  
  429. //----------------------------------------------------------------------------------------
  430. //    FW_CPrivGDIFont::SetFontStyle
  431. //----------------------------------------------------------------------------------------
  432.  
  433. void FW_CPrivGDIFont::SetFontStyle(FW_FontStyle fontStyle)
  434. {
  435.     if (fontStyle != fFontStyle || fStockObject || fGDIObject == NULL)
  436.     {
  437.         fStockObject = FALSE;
  438.         fFontStyle = fontStyle;
  439.         fChanged = TRUE;
  440.     }
  441. }
  442.  
  443. //----------------------------------------------------------------------------------------
  444. //    FW_CPrivGDIFont::SetFontSize
  445. //----------------------------------------------------------------------------------------
  446.  
  447. void FW_CPrivGDIFont::SetFontSize(short fontSize)
  448. {
  449.     if (fontSize != fFontSize || fStockObject || fGDIObject == NULL)
  450.     {
  451.         fStockObject = FALSE;
  452.         fFontSize = fontSize;
  453.         fChanged = TRUE;
  454.     }
  455. }
  456.  
  457. //----------------------------------------------------------------------------------------
  458. //    FW_CPrivGDIFont::CreateObject
  459. //----------------------------------------------------------------------------------------
  460.  
  461. HGDIOBJ FW_CPrivGDIFont::CreateObject()
  462. {
  463.     LOGFONT logFont;
  464.     FW_CMemoryManager::SetMemory(&logFont, sizeof(logFont), 0x00);
  465.     
  466.     HDC screenDC = ::GetDC(NULL);
  467.     logFont.lfHeight = -MulDiv(fFontSize, ::GetDeviceCaps(screenDC, LOGPIXELSY), 72);
  468.     ::ReleaseDC(NULL, screenDC);
  469.     
  470.     FW_PrimitiveStringCopy(fFontName, logFont.lfFaceName); 
  471.  
  472.     if (fFontStyle & FW_kItalic)
  473.         logFont.lfItalic = 0xFF;
  474.     if (fFontStyle & FW_kUnderline)
  475.         logFont.lfUnderline = 0xFF;
  476.     if (fFontStyle & FW_kStrikeOut)
  477.         logFont.lfStrikeOut = 0xFF;
  478.     if (fFontStyle & FW_kBold)
  479.         logFont.lfWeight = FW_BOLD;
  480.     
  481.     return ::CreateFontIndirect(&logFont);
  482. }
  483.  
  484.  
  485.  
  486. #endif
  487.